package org.bedracket.pgeg.api;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.registries.ForgeRegistries;
import org.bedracket.pgeg.PGEGMod;
import org.bedracket.pgeg.util.SafeTag;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public interface RuneLike extends ItemLike {

    static String getRuneTag(ItemStack stack) {
        return new SafeTag(stack, PGEGMod.MOD_ID, false).getString("Rune").orElse(null);
    }

    static void setRuneTag(ItemStack stack, String meta) {
        new SafeTag(stack, PGEGMod.MOD_ID, true).putString("Rune", meta);
    }

    static boolean equals(ItemStack i1, ItemStack i2) {
        return i1.getItem() == i2.getItem() && Objects.equals(getRuneTag(i1), getRuneTag(i2));
    }

    static void copyRuneTag(ItemStack source, ItemStack target) {
        RuneLike.setRuneTag(target, RuneLike.getRuneTag(source));
    }

    default ResourceLocation RuneResLoc(ItemStack stack) {
        return runeResLoc(getRuneTag(stack));
    }

    default ResourceLocation runeResLoc(String meta) {
        ResourceLocation key = Objects.requireNonNull(ForgeRegistries.ITEMS.getKey(asItem()));
        return new ResourceLocation(key.getNamespace(), key.getPath() + "/" + meta);
    }

    @Override
    default @NotNull Item asItem() {
        return (Item) this;
    }
}
